package com.benefm.multipar.pad.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.benefm.multipar.pad.AppConfig;
import com.benefm.multipar.pad.BaseApp;
import com.benefm.multipar.pad.R;
import com.benefm.multipar.pad.dashboard.BleDeviceListActivity;
import com.benefm.multipar.pad.dashboard.OnlineDeviceActivity;
import com.benefm.multipar.pad.databinding.ActivityLoginBinding;
import com.benefm.multipar.pad.http.HttpRequest;
import com.benefm.multipar.pad.http.HttpResult;
import com.benefm.multipar.pad.model.LoginInfo;
import com.benefm.multipar.pad.util.ACache;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends BaseActivity {

    private ActivityLoginBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_login);

        initData();

        addListener();

    }

    private void initData() {

        if (BaseApp.isDebug)
            binding.edAccount.setText("duocantest11");
        else
            binding.edAccount.setText("duocan");
        binding.edPwd.setText("duocan123");

        String mName = ACache.get(this).getString(AppConfig.LOGIN_USER_NAME);
        String mPwd = ACache.get(this).getString(AppConfig.LOGIN_USER_PWD);
        if (!TextUtils.isEmpty(mName))
            binding.edAccount.setText(mName);
        if (!TextUtils.isEmpty(mPwd))
            binding.edPwd.setText(mPwd);

    }


    private void addListener() {
        binding.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                // 显示密码
                binding.edPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                binding.edPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }

        });

        binding.btnLogin.setOnClickListener(v -> {

            String username = Objects.requireNonNull(binding.edAccount.getText()).toString();
            String password = Objects.requireNonNull(binding.edPwd.getText()).toString();

            if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                Toast.makeText(BaseApp.baseApp, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            login(username, password);
        });


        binding.bleMode.setOnClickListener(v -> {
            startActivity(new Intent(LoginActivity.this, BleDeviceListActivity.class));
            finish();
        });

    }


    private void login(String username, String password) {
        showLoading();
        HttpRequest.login(username, password, new Callback<HttpResult<LoginInfo>>() {
            @Override
            public void onResponse(Call<HttpResult<LoginInfo>> call, Response<HttpResult<LoginInfo>> response) {
                hideLoading();
                if (response.body() != null && response.isSuccessful()) {
                    HttpResult<LoginInfo> result = response.body();
                    if (AppConfig.SUCCESS.equals(result.resultCode)) {
                        LoginInfo info = result.resultData;
                        System.out.println(info.toString());
                        ACache.get(BaseApp.baseApp).put(AppConfig.LOGIN_INFO, info);
                        ACache.get(LoginActivity.this).put(AppConfig.LOGIN_USER_NAME, username);
                        ACache.get(LoginActivity.this).put(AppConfig.LOGIN_USER_PWD, password);

                        startActivity(new Intent(LoginActivity.this, OnlineDeviceActivity.class));
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, result.resultMsg, Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<HttpResult<LoginInfo>> call, Throwable t) {
                hideLoading();
            }
        });
    }

}
